package com.gx.util.commonutil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.net.URL;

/**
 * 根据经纬度转为具体位置
 * Created by Administrator on 2016/12/5.
 */
@Slf4j
public class GPSUtil {
    public static String getAdd(String lon, String lat ){
        //lat 小  log  大
        //参数解释: 纬度,经度 type 001 (100代表道路，010代表POI，001代表门址，111可以同时显示前三项)
        String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+lon+"&type=010";
        String addName;
        try {
            URL url = new URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            StringBuilder res = new StringBuilder();
            while ((line = in.readLine()) != null) {
                res.append(line).append("\n");
            }
            in.close();
            JSONObject jsonObject = JSON.parseObject(res.toString());
            JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("addrList"));
            JSONObject j_2 = JSON.parseObject(jsonArray.get(0).toString(), JSONObject.class);
            addName = j_2.getString("admName") + j_2.getString("name");
            if("".equals(addName)){
                log.error("阿里云GPS解析失败，lon:{}, lat:{}", lon, lat);
                addName = baiduAnaly(lon, lat);
            }
        } catch (Exception e) {
            log.error("阿里云GPS解析异常，lon:{}, lat:{}", lon, lat);
            addName = baiduAnaly(lon, lat);
        }
        return addName;
    }


    public static String baiduAnaly(String lon, String lat ){
        String urlString = "http://api.map.baidu.com/geocoder?output=json&location="+lat+","+lon;
        try {
            URL url = new URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            StringBuilder res = new StringBuilder();
            while ((line = in.readLine()) != null) {
                res.append(line).append("\n");
            }
            in.close();
            JSONObject jsonObject = JSON.parseObject(res.toString());
            if("OK".equals(jsonObject.getString("status"))){
                JSONObject result = JSON.parseObject(jsonObject.getString("result"));
                return result.getString("formatted_address");
            }
        } catch (Exception e) {
            log.error("百度GPS解析失败，lon:{}, lat:{}", lon, lat);
        }
        return lat+","+lon;
    }

//    public static void main(String[] args) {
////        System.out.println(getAdd("127.442571", "50.210438"));
//        System.out.println(getAdd("116.4363894314236", "39.85501302083333"));
//    }
}
